package com.cloud.phone.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cloud.phone.R;
import com.cloud.phone.bean.ListItemBean;

import java.util.List;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
    public List<ListItemBean> itemBeans;
    public OnItemClickListener onItemClickListener;

    public ItemAdapter(List<ListItemBean> itemBeans) {
        this.itemBeans = itemBeans;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_list, viewGroup, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder itemViewHolder, @SuppressLint("RecyclerView") int i) {
        itemViewHolder.tvContent.setText(itemBeans.get(i).getContent());
        itemViewHolder.ivImage.setImageResource(itemBeans.get(i).getResImage());
        itemViewHolder.llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onChildItemClick(i);
                }
            }
        });

    }


    public interface OnItemClickListener{
        void onChildItemClick(int position);
    }

    @Override
    public int getItemCount() {
        return itemBeans.size();
    }

    // 定义一个 ViewHolder 类
    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        public  TextView tvContent;
        public  ImageView ivImage;
        public   LinearLayout llItem;


        public ItemViewHolder(View view) {
            super(view);
            ivImage = (ImageView) view.findViewById(R.id.iv_image);
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            llItem = (LinearLayout) view.findViewById(R.id.ll_item);
        }
    }
}
